<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件

# 获取当前时间
function now_time()
{
    return date("Y-m-d H:i:s",time());
}
# 获取ip
function ip()
{
    return request()->ip();
}
# 封装Get请求
function get($url){
    $oCurl = curl_init();
    if(stripos($url,"https://")!==FALSE){
        curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
    }
    curl_setopt($oCurl, CURLOPT_URL, $url);
    curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );
    $sContent = curl_exec($oCurl);
    $aStatus = curl_getinfo($oCurl);
    curl_close($oCurl);
    if(intval($aStatus["http_code"])==200){
        return $sContent;
    }else{
        return false;
    }
}
/**
 * PHP发送Json对象数据
 *
 * @param $url 请求url
 * @param $jsonStr 发送的json字符串
 * @return array
 */
function http_post_json($url, $jsonStr)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json; charset=utf-8',
            'Content-Length: ' . strlen($jsonStr)
        )
    );
    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    return $response;
}

# 根据ip返回具体地区
function getAddress($ip)
{
    $data = json_decode(get('http://ip.taobao.com/service/getIpInfo.php?ip='.$ip),true);
    $address = "{$data['data']['country']}"."{$data['data']['isp']}"."{$data['data']['region']}"."{$data['data']['city']}";
    return $address;
}
// 随机生成32位
function getRandChar($length)
{
    $str = null;
    $strPol = "ABCDEFGHIJKLNMOPQRSTUPWXYZ1234567890abcdefghijklnmopqrstupwsyz";
    $max = strlen($strPol) - 1;
    for($i = 0; $i < $length; $i++)
    {
        $str .= $strPol[rand(0,$max)];
    }
    return $str;
}
// 删除数组中value为空的元素
function unsetArrNull($data)
{
    foreach ($data as $key => $value){
        if($value == ""){
            unset($data[$key]);
        }
    }
    return $data;
}

/**
 * 执行一个cURL会话
 * @param string $url
 * @param array $param
 * @param array $http_header
 * @param int $execution_time
 * @return mixed
 */
function http_post($url = '', $param = [], $http_header = [], $execution_time = 60)
{

    // 初始化一个url会话
    $curl = curl_init();
    if (!empty($http_header)) curl_setopt($curl,CURLOPT_HTTPHEADER, $http_header);
    // 设置想用PHP取回的url地址
    curl_setopt($curl, CURLOPT_URL, $url);
    // 终止从服务端进行验证
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    // 检查服务器证书
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    // 设置cURL允许执行的最长秒数,2秒
    curl_setopt($curl, CURLOPT_TIMEOUT, $execution_time);
    // 判断传输的数据是否为空
    if (!empty($param)) {
        // 发送一个常规的POST请求
        curl_setopt($curl, CURLOPT_POST, 1);
        // POST内容
        curl_setopt($curl, CURLOPT_POSTFIELDS, $param);
    }
    // 文件流的形式返回，而不是直接输出
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    // 执行curl会话将结果复制给变量
    $output = curl_exec($curl);
    // 关闭curl会话
    curl_close($curl);
    // 返回执行curl会话得到的结果
    return $output;
}
function LogRecord()
{
   return 'Operation record : '.' [module => ' .\think\facade\Request::module().'][ controller => '.\think\facade\Request::controller().' ][action => '.\think\facade\Request::action().']';
}


/**
 * 执行一个cURL会话
 */
function http($url = '', $param = [], $data_type = 0, $http_header = [], $execution_time = 60)
{

    $curl = curl_init();
    $options = [
        CURLOPT_URL            => $url,
        CURLOPT_SSL_VERIFYPEER => FALSE,
        CURLOPT_SSL_VERIFYHOST => FALSE,
        CURLOPT_TIMEOUT        => $execution_time,
        CURLOPT_RETURNTRANSFER => 1
    ];
    if (!empty($http_header)) $options[CURLOPT_HTTPHEADER] = $http_header;
    if (!empty($param)) {
        $options[CURLOPT_POST]       = 1;
        $options[CURLOPT_POSTFIELDS] = $param;
    }
    curl_setopt_array($curl, $options);
    $curl_exec = curl_exec($curl);
    if($curl_exec != true) exception("调用API时发生错误[" . curl_error($curl) . "]");
    curl_close($curl);
    switch ($data_type) {
        case 0:
            $curl_exec = json_decode($curl_exec, true);
            break;
        case 1:
            libxml_disable_entity_loader(true);
            $curl_exec = json_decode(json_encode(simplexml_load_string($curl_exec, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
            break;
        case 2:
            // 不处理直接返回
            break;
        default:
            exception('API结果处理类型出现异常');
            break;
    }
    return $curl_exec;
}
